package Websites;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import KUtilities.KLogger;

public class SMS440 extends SMSService{
	
    private String viewState,viewStateEncrypted,eventValidation,gsmSender,cdmaSender;
    
    public SMS440(KLogger logger){
    	super(logger,"www.sms440.com");
    	setSendData("http://www.sms440.com/SMSCenter_Sched.aspx","http://www.sms440.com/SMSCenter_Sched.aspx","Send Successful");
    	setLoginData("http://www.sms440.com/SMSLogin.aspx","http://www.sms440.com/SMSLogin.aspx");
    }
    
    protected String getLoginContent(String username, String password) {
		String loginContent;
		loginContent="txtEMail="+username;
		loginContent+="&txtPassword="+password;
		return loginContent;
	}

    protected boolean isValidLogin(HttpURLConnection loginConnection){
    	String location=loginConnection.getHeaderField("Location");
    	if(location==null || location.indexOf("SMSCenter_Sched.aspx")==-1)
        	return false;
		return true;
	}
    
	protected void grabRequirements() {
		thread=new Thread(){
        	public void run(){
        		try{
        			URL location=new URL("http://www.sms440.com/SMSCenter_Sched.aspx");
                    HttpURLConnection actionConnection=(HttpURLConnection)location.openConnection();
                    
                    actionConnection.setRequestProperty("Host",host);
                    actionConnection.setRequestProperty("User-Agent","Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
                    actionConnection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    actionConnection.setRequestProperty("Accept-Language","en-us,en;q=0.5");
                    actionConnection.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                    actionConnection.setRequestProperty("Connection","keep-alive");
                    actionConnection.setRequestProperty("Cookie", cookie);
                    actionConnection.setRequestMethod("GET");
                    actionConnection.setInstanceFollowRedirects(false);
                    	                    
                    BufferedReader actionReader=new BufferedReader(new InputStreamReader(actionConnection.getInputStream()));
                    StringBuilder response =new StringBuilder();
                    String temp="";
                    while((temp=actionReader.readLine())!=null){
                        response.append(temp+"\n");
                    }
                    actionReader.close();
                    String searchStr="<input type=\"hidden\" name=\"__VIEWSTATE\" id=\"__VIEWSTATE\" value=";
                    int i=response.indexOf(searchStr)+searchStr.length()+1;
                    viewState=URLEncoder.encode(response.substring(i,response.indexOf("\"", i)),"UTF-8");
                    searchStr="<input type=\"hidden\" name=\"__VIEWSTATEENCRYPTED\" id=\"__VIEWSTATEENCRYPTED\" value=";
                    i=response.indexOf(searchStr,i)+searchStr.length()+1;
                    viewStateEncrypted=URLEncoder.encode(response.substring(i,response.indexOf("\"", i)),"UTF-8");
                    searchStr="<input type=\"hidden\" name=\"__EVENTVALIDATION\" id=\"__EVENTVALIDATION\" value=";
                    i=response.indexOf(searchStr,i)+searchStr.length()+1;
                    eventValidation=URLEncoder.encode(response.substring(i,response.indexOf("\"", i)),"UTF-8");
                    searchStr="<input type=\"hidden\" name=\"hdnGSMSender\" id=\"hdnGSMSender\" value=";
                    i=response.indexOf(searchStr,i)+searchStr.length()+1;
                    gsmSender=URLEncoder.encode(response.substring(i,response.indexOf("\"", i)),"UTF-8");
                    searchStr="<input type=\"hidden\" name=\"hdnCDMASender\" id=\"hdnCDMASender\" value=";
                    i=response.indexOf(searchStr,i)+searchStr.length()+1;
                    cdmaSender=URLEncoder.encode(response.substring(i,response.indexOf("\"", i)),"UTF-8");
        		}catch(Exception e){}
        	}
        };
        thread.start();
	}
    
    protected String getSendContent(String number,String message)
    {
    	String sendContent;
        sendContent="__VIEWSTATE="+viewState;
        sendContent+="&__VIEWSTATEENCRYPTED="+viewStateEncrypted;
        sendContent+="&__EVENTVALIDATION="+eventValidation;
        sendContent+="&hdnGSMSender="+gsmSender;
        sendContent+="&hdnCDMASender="+cdmaSender;
        sendContent+="&txtphnofromlist1="+number;
        sendContent+="&txtMessage="+message;
        sendContent+="&txtcharcount="+message.length()+" characters";
        sendContent+="&SchGp=rdbNow";
        sendContent+="&ddlHour=Hour";
        sendContent+="&ddlMinute=Minute";
        sendContent+="&ddlMonth=Month";
        sendContent+="&ddlDay=Day";
        sendContent+="&ddlYear=Year";
        sendContent+="&butSendSMS=SendSMS";
        return sendContent;
    }
        
    public boolean logout()
    {
    	return logout("http://sms440.com/logout.aspx");
    }
    
	public String getServiceName() {
		return "SMS440";
	}

	public int getMaxChar() {
		return 400;
	}
}
